    //
//  ShapeLayerController.m
//  LayerStudy
//
//  Created by longhuihu on 11-6-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ShapeLayerController.h"
#import <QuartzCore/QuartzCore.h>


@implementation ShapeLayerController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

-(void)segmentSelectChanged:(id)sender
{
	UISegmentedControl *segment = sender;
	if (segment.selectedSegmentIndex==0) 
	{
		CGMutablePathRef path = CGPathCreateMutable();
		CGPathMoveToPoint(path, nil, 10, 100);
		CGPathAddLineToPoint(path, nil, 300, 100);
		CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
		animation.duration=3.0;
		animation.fromValue=(id)shapeLayer.path;
		animation.toValue=(id)path;
		[shapeLayer addAnimation:animation forKey:@"pathAnimation"];
		shapeLayer.path = path;
		CGPathRelease(path);
	} 
	else if (segment.selectedSegmentIndex==1) 
	{
		CGMutablePathRef path = CGPathCreateMutable();
		
		CGFloat startX = 10;
		CGFloat width  = 300;
		
		CGFloat startY = 100;
		CGFloat height = 100;
		
		CGPathMoveToPoint(path, NULL, startX, startY);
		CGFloat x;
		for ( x = -2*M_PI; x <= 2*M_PI; x += .15 )
		{
			// find the value that we will plot
			CGFloat fx =  sin(x);
			
			// convert the x value to a x coordinate
			CGFloat rectX = (((x + 2*M_PI)/(4*M_PI)) * width) + startX;
			
			// convert the f(x) value to a y coordinate
			CGFloat rectY = ((fx/2) * height) + startY;
			
			CGPathAddLineToPoint(path, NULL, rectX, rectY);        
		}
		CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
		animation.duration=3.0;
		animation.fromValue=(id)shapeLayer.path;
		animation.toValue=(id)path;
		[shapeLayer addAnimation:animation forKey:@"pathAnimation"];
		shapeLayer.path = path;
		CGPathRelease(path);
	} else {
		UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(5, 5, 310, 290)];
		[path moveToPoint:CGPointMake(10, 10)];
		[path addArcWithCenter:CGPointMake(100, 100)
						radius:100 startAngle:0.8 endAngle:1.5 clockwise:YES];
		
		CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
		animation.duration=3.0;
		animation.fromValue=(id)shapeLayer.path;
		animation.toValue=(id)[path CGPath];
		[shapeLayer addAnimation:animation forKey:@"pathAnimation"];
		shapeLayer.path = [path CGPath];
	}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = @"shapeLayer";
	
	// set up a rounded border
    CALayer *layer = [self.view layer];

    layer.borderColor = [[UIColor blackColor] CGColor];
    layer.borderWidth = 3.0f;
    layer.cornerRadius = 4.0f;
    
	
	shapeLayer = [CAShapeLayer layer];
	shapeLayer.frame = CGRectMake(0, 0, 320, 300);
	[layer addSublayer:shapeLayer];
	[shapeLayer setFillColor:[[UIColor whiteColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor blueColor] CGColor]];
    [shapeLayer setLineCap:kCALineCapRound];
    [shapeLayer setLineWidth:4.0f];
	
	
	UISegmentedControl *set = [[UISegmentedControl alloc] initWithFrame:CGRectMake(25, 320, 270, 40)];
	[set insertSegmentWithTitle:@"bezier" atIndex:0 animated:NO];
	[set insertSegmentWithTitle:@"sin" atIndex:0 animated:NO];
	[set insertSegmentWithTitle:@"line" atIndex:0 animated:NO];
	[set addTarget:self action:@selector(segmentSelectChanged:) forControlEvents:UIControlEventValueChanged];
	[self.view addSubview:set];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
